package com.zb.frame.modules.sys.controller;


import com.alibaba.fastjson.JSON;
import com.zb.frame.base.model.R;
import com.zb.frame.modules.common.controller.AbstractController;
import com.zb.frame.modules.monitor.enums.LogModule;
import com.zb.frame.modules.monitor.enums.LogType;
import com.zb.frame.modules.monitor.service.LogService;
import com.zb.frame.modules.sys.entity.Menu;
import com.zb.frame.modules.sys.service.MenuService;
import com.zb.frame.modules.sys.service.RelRoleMenuService;
import com.zb.frame.modules.sys.vo.MenuTreeVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/sys/menu")
public class MenuController extends AbstractController {

    @Autowired
    MenuService menuService;

    @Autowired
    RelRoleMenuService roleMenuService;

    @Autowired
    LogService logService;


    @ApiOperation(value = "getMenus", notes = "获取资源列表信息")
    @RequiresPermissions("sys:menu:view")
    @GetMapping("/list")
    public R getMenus() {
        List<MenuTreeVO> allList = menuService.getAllList();
        return R.ok(allList);
    }

    @ApiOperation(value = "getMenu", notes = "根据资源Id获取资源信息")
    @ApiImplicitParam(name = "menuId", value = "资源Id", dataType = "Number")
    @GetMapping("/info/{id}")
    @RequiresPermissions("sys:menu:view")
    public R getMenu(@PathVariable Long id) {
        Menu menu = menuService.getMenuById(id);
        return (menu != null) ? R.ok(menu) : R.error(404, "","失败，资源不存在！");
    }

    @ApiOperation(value = "newMenu", notes = "新增资源信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "menuName", value = "菜单名称", dataType = "String"),
        @ApiImplicitParam(name = "menuAlias", value = "菜单别名", dataType = "String"),
        @ApiImplicitParam(name = "menuIcon", value = "icon图标", dataType = "String"),
        @ApiImplicitParam(name = "menuType", value = "类型1-目录2-菜单3-操作", dataType = "Number"),
        @ApiImplicitParam(name = "menuPath", value = "路径", dataType = "String"),
        @ApiImplicitParam(name = "menuPerm", value = "授权标识", dataType = "String"),
        @ApiImplicitParam(name = "menuDesc", value = "描述", dataType = "String"),
        @ApiImplicitParam(name = "menuOrder", value = "排序", dataType = "Number")
    })
    @PostMapping("/info")
    @RequiresPermissions("sys:menu:add")
    public R newMenu(@RequestBody Menu data) {
        String menuName = data.getMenuName();
        boolean isExist = menuService.isExistName(menuName);
        if (isExist) return R.error("失败，资源名称已存在！");
        Menu menu = menuService.addResource(data);
        if (menu != null) {
            // 添加日志
            String newValue = JSON.toJSONString(menu);
            logService.addLog(LogModule.SYSMENU, LogType.ADD,"", newValue, String.valueOf(menu.getMenuId()), this.getUserId(),"新增资源[" + menu.getMenuName() + "]成功");
            return R.ok("成功！",menu.getMenuId());
        } else {
            return R.error("失败！");
        }
    }

    @ApiOperation(value = "updateMenu", notes = "修改资源信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "menuId", value = "资源Id", dataType = "Number"),
        @ApiImplicitParam(name = "menuName", value = "资源名称", dataType = "String"),
        @ApiImplicitParam(name = "menuAlias", value = "资源别名", dataType = "String"),
        @ApiImplicitParam(name = "menuIcon", value = "icon图标", dataType = "String"),
        @ApiImplicitParam(name = "menuType", value = "类型1-目录2-菜单3-操作", dataType = "Number"),
        @ApiImplicitParam(name = "menuPath", value = "路径", dataType = "String"),
        @ApiImplicitParam(name = "menuPerm", value = "授权标识", dataType = "String"),
        @ApiImplicitParam(name = "menuDesc", value = "描述", dataType = "String"),
        @ApiImplicitParam(name = "menuOrder", value = "排序", dataType = "Number")
    })
    @PutMapping("/info/{id}")
    @RequiresPermissions("sys:menu:edit")
    public R updateMenu(@PathVariable Long id, @RequestBody Menu data) {
        data.setMenuId(id);
        String oldValue = JSON.toJSONString(data);
        Menu newMenu = menuService.updateRole(data);
        if (newMenu == null) return R.error("失败！");
        String newValue = JSON.toJSONString(newMenu);
        // 添加日志
        logService.addLog(LogModule.SYSMENU, LogType.UPDATE, oldValue, newValue, String.valueOf(newMenu.getMenuId()), this.getUserId(),"修改资源[" + data.getMenuName() + "]成功");
        return R.ok("成功！");
    }

    @ApiOperation(value = "deleteMenu", notes = "删除资源")
    @ApiImplicitParam(name = "menuId", value = "删除主键", dataType = "Number")
    @DeleteMapping("/info/{id}")
    @RequiresPermissions("sys:menu:del")
    public R deleteMenu(@PathVariable Long id){
        boolean isExist = menuService.isExistChildren(id);
        if (isExist) return R.error("失败，该资源下有子资源未删除！");
        roleMenuService.deleteByMenuId(id);
        menuService.removeById(id);
        // 添加日志
        logService.addLog(LogModule.SYSMENU, LogType.DEL, "", "", String.valueOf(id), this.getUserId(),"删除资源成功");
        return R.ok("成功！");
    }

    @ApiOperation(value = "getMenuRelRole", notes = "删除资源前查询与角色的关联情况")
    @ApiImplicitParam(name = "menuId", value = "资源Id", dataType = "Number")
    @GetMapping("/relRole/{id}")
    @RequiresPermissions("sys:menu:view")
    public R getMenuRelRole(@PathVariable Long id) {
        List<String> data = roleMenuService.getRolesByMenuId(id);
        return R.ok(data);
    }


}
